package com.example.sangong.kafka;

import cn.hutool.json.JSONUtil;
import com.example.sangong.entity.SgLog;
import com.example.sangong.service.SgLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 异步消费消息，保存日志
 */
@Slf4j
@Component
public class MyKafkaListener {
    @Autowired
    private SgLogService sgLogService;

    @KafkaListener(topics = {"${kafka.log-topic}"})
    public void listen(String data) {
        log.info("kafka收到数据为:{}", data);
        SgLog sgLog = JSONUtil.toBean(data, SgLog.class);
        sgLogService.addLog(sgLog);

    }
}
